package com.google.code.irouter.portal.client.dashboard.main.center.managers.relation.browser.editor;


import java.util.ArrayList;
import java.util.List;

import com.google.code.irouter.portal.client.AppConstants;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.relation.Browser;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.relation.browser.editor.detail.Command;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.relation.browser.editor.detail.Consumer;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.relation.browser.editor.detail.Guide;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.relation.browser.editor.detail.command.Name;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.relation.browser.editor.detail.command.Save;
import com.google.code.irouter.portal.client.dashboard.main.north.menu.switcher.Solutions;
import com.google.code.irouter.portal.client.rpc.relation.RelationService;
import com.google.code.irouter.portal.client.rpc.relation.RelationServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.Ext;
import com.gwtext.client.core.Position;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.TabPanel;

public final class Detail extends TabPanel {
	
	private static final AppConstants CONSTANTS = GWT.create(AppConstants.class);
	private final RelationServiceAsync relationService = GWT.create(RelationService.class);
	private Guide guide;
	private Consumer consumer;
	private String relationshipId;
	
	public String getRelationshipId() {
		return relationshipId;
	}

	public void setRelationshipId(String relationshipId) {
		this.relationshipId = relationshipId;
	}

	public Detail() {
		super();
		this.setId(Detail.class.getName());
		
		this.setTabPosition(Position.BOTTOM);
		this.setBorder(false);		
		guide = new Guide();
		consumer = new Consumer();
        this.add(guide);
        this.add(consumer);
		this.setActiveTab(0);
        this.setTopToolbar(new Command());  

	}
	
	public void save() {		
		String name = ((Name) Ext.getCmp(Name.class.getName())).getValueAsString();
		if(name.trim().length() == 0) {
			MessageBox.alert(CONSTANTS.com_google_code_irouter_portal_client_common_confirm_input_name_please());
			return;
		}
		
		Record[] recordGuide = guide.getTarget().getStore().getRecords();
		Record[] recordConsumer = consumer.getTarget().getStore().getRecords();
		
		List<Long> guideIdList = new ArrayList<Long>();
		List<Long> consumerIdList = new ArrayList<Long>();
		for(Record record : recordGuide)
			guideIdList.add(Long.valueOf(record.getAsString("key")));
		for(Record record : recordConsumer)
			consumerIdList.add(Long.valueOf(record.getAsString("key")));
		if(guideIdList.size() == 0) {
			MessageBox.alert(CONSTANTS.com_google_code_irouter_portal_client_common_confirm_set_guide_please());
			return;
		}
		if(consumerIdList.size() == 0) {
			MessageBox.alert(CONSTANTS.com_google_code_irouter_portal_client_common_confirm_set_consumer_please());
			return;
		}
		
		getEl().mask(CONSTANTS.com_google_code_irouter_portal_client_common_processing(), true);
		relationService.save(Cookies.getCookie(Solutions.COOKIE_SOLUTION), relationshipId, name, guideIdList, consumerIdList, new AsyncCallback<Boolean>() {
			public void onFailure(Throwable caught) {
				// TODO
					getEl().unmask();
				}
				public void onSuccess(Boolean result) {
					getEl().unmask();					
					((Browser) Ext.getCmp(Browser.class.getName())).setActiveItem(0);
				}
			}
		);
	}
	
	public enum Children {GUIDE, CONSUMER};
	private boolean consumerEnable = true;
	private boolean guideEnable = true;
	
	public void setGlobalDisable() {
		consumerEnable = false;
		guideEnable = false;
		guide.disable();
		consumer.disable();
		((Name) Ext.getCmp(Name.class.getName())).disable();
		((Save) Ext.getCmp(Save.class.getName())).disable();
	}
	
	public void setGlobalEnable(Children child) {
		if(child.equals(Children.GUIDE)) 
			guideEnable = true;
		if(child.equals(Children.CONSUMER)) 
			consumerEnable = true;
		if(guideEnable && consumerEnable) {
			guide.enable();
			consumer.enable();
			((Name) Ext.getCmp(Name.class.getName())).enable();
			((Save) Ext.getCmp(Save.class.getName())).enable();
		}
	}

}
